package com.example.demoplus;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private DatabaseHelper databaseHelper;
    //    控件属性
    private RelativeLayout rlTitleBar;
    private EditText etUsername, etPassword, etEnPassword;
    private ImageButton ibRegister;
    private ImageView ivBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
//        标题返回
        titleBack();
//        找到控件方法
        initView();
        databaseHelper = new DatabaseHelper(this);
    }

    @SuppressLint("ResourceAsColor")
    private void titleBack() {
//        找到控件
        rlTitleBar = findViewById(R.id.rl_title_bar);
        ivBack = findViewById(R.id.iv_back);
//        rlTitleBar.setBackgroundColor(R.color.blue_color);
//        设置返回按钮可见
        ivBack.setVisibility(View.VISIBLE);
//        返回事件
        ivBack.setOnClickListener(this);
    }
//跳转到登录页面
    private void toLoginActivity() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void initView() {
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_passwd);
        etEnPassword = findViewById(R.id.et_en_passwd);
        ibRegister = findViewById(R.id.ib_register);
//        注册按钮监听事件
        ibRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
//            点击注册按钮
            case R.id.ib_register:
                //获取用户输入的用户名、密码、确认密码
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                String enterPassword = etEnPassword.getText().toString().trim();
                //注册验证
                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(enterPassword)) {
//                    如果两次密码一致
                    if (password.equals(enterPassword)) {
                        //将用户名和密码加入到数据库中
                        databaseHelper.add(username, password);
//                        跳转到登录界面
                        toLoginActivity();
                        Toast.makeText(this,  "注册成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "请确认密码,注册失败", Toast.LENGTH_SHORT).show();
                    }
                }else if (!TextUtils.isEmpty(username)){    //如果只有用户名
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)){    //如果只有用户名和密码
                    Toast.makeText(this, "请确认密码", Toast.LENGTH_SHORT).show();
                }else if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(enterPassword)){   //如果只有密码和确认密码
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }
                break;
//                点击返回按钮
            case R.id.iv_back:
                toLoginActivity();
                break;
        }
    }
}